package pokerserver.core;

import java.util.ArrayList;
import java.util.Collections;

import common.Card;
import common.Card.Suit;
import common.Card.Rank;

/**
 * A class representing a deck. Containing 52 cards, all of them with different
 * ranks and/or suits.
 * 
 * @author Aron Manucheri
 */
public class Deck {

	// The current cards in the deck.
	private ArrayList<Card> cards = new ArrayList<Card>();

	/**
	 * Constructor. Creates a new Deck, adds 52 cards to it.
	 * A new deck has when created not shuffled cards.
	 */
	public Deck() {
		// Add cards to the deck.
		for (Suit suit : Suit.values()) {
			for (Rank value : Rank.values()) {
				cards.add(new Card(value, suit));
			}
		}
	}

	/**
	 * Shuffles the cards in the deck.
	 */
	public void shuffle() {
		Collections.shuffle(cards);
	}

	/**
	 * @return The total amount of cards currently in the deck.
	 * */
	public int getTotalCards() {
		return cards.size();
	}

	/**
	 * Draw a card.
	 * @return a card and remove it from the deck.
	 */
	public Card drawCard() {
		Card card = cards.get(0);
		cards.remove(0);
		return card;
	}

	@Override
	public String toString() {
		return cards.toString();
	}


	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result + ((cards == null) ? 0 : cards.hashCode());
		return result;
	}


	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		Deck other = (Deck) obj;
		if (cards == null) {
			if (other.cards != null)
				return false;
		} else if (!cards.equals(other.cards))
			return false;
		return true;
	}

}
